package br.com.dlm.simuladoitil.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import br.com.dlm.simuladoitil.R;
import br.com.dlm.simuladoitil.basicas.Categoria;
import br.com.dlm.simuladoitil.fragments.QuestaoFragment;

public class QuestaoActivity extends GeralActivity {

    public static final String EXTRA_CATEGORIA = "EXTRA_CATEGORIA";
    public static final String EXTRA_POSICAO = "EXTRA_POSICAO";
    public static final String TAG_FRAGMENT_QUESTAO = "TAG_FRAGMENT_QUESTAO";
    public static final String VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT = "VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT";

    private QuestaoFragment questaoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questao);

       if (conteudo().getObjeto(VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT) == null) {
            Categoria categoria = (Categoria) getIntent().getSerializableExtra(EXTRA_CATEGORIA);
            int posicao = getIntent().getIntExtra(EXTRA_POSICAO, 0);

            questaoFragment = QuestaoFragment.newInstance(categoria, posicao);
       } else {
            questaoFragment = (QuestaoFragment) conteudo().getObjeto(VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT);
       }

       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       fragmentTransaction.replace(R.id.containerQuestao, questaoFragment, TAG_FRAGMENT_QUESTAO);
       fragmentTransaction.commit();

       setResult(RESULT_CANCELED);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        conteudo().putObjeto(VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT, questaoFragment);
    }
}
